//
//  MCDataTool.m
//  TLYL
//
//  Created by MC on 2017/6/20.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCDataTool.h"
#import "MCUserDefinedLotteryCategoriesModel.h"

@implementation MCDataTool
+ (void)changeYKLabelColor:(UILabel *)label{
    float f = [label.text floatValue];
    if (f == 0) {
        label.textColor = RGB(102, 102, 102);
    } else if (f > 0){
        label.textColor = [UIColor greenColor];
    }else {
        label.textColor = [UIColor redColor];
    }
}
+(void)MC_SaveMarr:(NSArray *)marr withID:(NSString *)IDStr{
    
    //将模型数组转化为字典数组
    NSMutableArray *array=[NSMutableArray array];
    for (int i=0; i<marr.count; i++) {
        MCUserDefinedLotteryCategoriesModel *cellModel=marr[i];
        NSDictionary *dict=cellModel.mj_keyValues;
        [array addObject:dict];
    }
    //保存数组在本地
    NSString * docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
    
    NSString * fileName = [docDir stringByAppendingPathComponent:IDStr];
    
    [array writeToFile:fileName atomically:YES];
    
    
}

+(NSMutableArray *)MC_GetMarr_withID:(NSString *)IDStr{
    //取出本地的字典数组
    NSString * docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
    NSString * fileName = [docDir stringByAppendingPathComponent:IDStr];
    NSArray * models = [NSArray arrayWithContentsOfFile:fileName];
    //将取出来的字典数组转化为模型数组
    NSMutableArray *arrayModel=[NSMutableArray array];
    for (int i=0; i<models.count; i++) {
        
        NSDictionary *dict=models[i];
        //字典转模型
        MCUserDefinedLotteryCategoriesModel *cellModel=[MCUserDefinedLotteryCategoriesModel mj_objectWithKeyValues:dict];
        [arrayModel addObject:cellModel];
    }
    
    return arrayModel;
    
}
/*
 * 获取LotteryJSON_CZHelper.json的字典
 */
+(NSDictionary *)MC_GetDic_CZHelper{
    NSData *JSONData_CZHelper = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LotteryJSON_CZHelper.json" ofType:nil ]];
    NSDictionary*dic_CZHelper = [NSJSONSerialization JSONObjectWithData:JSONData_CZHelper options:NSJSONReadingAllowFragments error:nil];
    return dic_CZHelper;
}

/*
 * 获取LotteryJSON_CZ.json的字典
 */
+(NSDictionary *)MC_GetDic_CZ{
    NSData *JSONData_CZ = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LotteryJSON_CZ.json" ofType:nil ]];
    NSDictionary*dic_CZ = [NSJSONSerialization JSONObjectWithData:JSONData_CZ options:NSJSONReadingAllowFragments error:nil];
    return dic_CZ;
}

/*
 * 获取LotteryJSON_WFHelper.json的字典
 */
+(NSDictionary *)MC_GetDic_WFHelper{
    NSData *JSONData_WF = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LotteryJSON_WFHelper.json" ofType:nil ]];
    
    NSDictionary*dic_WFHelper = [NSJSONSerialization JSONObjectWithData:JSONData_WF options:NSJSONReadingAllowFragments error:nil];
    
    return dic_WFHelper;
}

/*
 * 获取省份列表
 */
+(NSDictionary *)MC_GetDic_Province{
    NSData *JSONData_Province = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ProvinceSelectedJSON_Helper.json" ofType:nil ]];
    
    NSDictionary*dic_Province = [NSJSONSerialization JSONObjectWithData:JSONData_Province options:NSJSONReadingAllowFragments error:nil];
    
    return dic_Province;
}

/*
 * 获取城市列表
 */
+(NSDictionary *)MC_GetDic_City{
    NSData *JSONData_City = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CitySelectedJSON_Helper.json" ofType:nil ]];
    
    NSDictionary*dic_City = [NSJSONSerialization JSONObjectWithData:JSONData_City options:NSJSONReadingAllowFragments error:nil];
    
    return dic_City;
}


/*
 * 获取银行列表
 */
+(NSDictionary *)MC_GetDic_Bank{
    NSData *JSONData_Bank = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BankJSON_Helper.json" ofType:nil ]];
    
    NSDictionary*dic_Bank = [NSJSONSerialization JSONObjectWithData:JSONData_Bank options:NSJSONReadingAllowFragments error:nil];
    
    return dic_Bank;
}

/*
 * 获取充值银行名称列表
 */
+(NSDictionary *)MC_GetDic_Recharge{
    NSData *JSONData_Recharge = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"RechargeJSON_Helper.json" ofType:nil ]];
    
    NSDictionary*dic_Recharge = [NSJSONSerialization JSONObjectWithData:JSONData_Recharge options:NSJSONReadingAllowFragments error:nil];
    
    return dic_Recharge;
}
//获取用户返点
+(NSMutableArray * )GetShowBetRebateMarryWithModel:(MCBasePWFModel *)WFModel{
    
    NSMutableArray * marr=[[NSMutableArray alloc]init];
    
    /*
     * 商户返点
     */
    int  shangHuRebate = [WFModel.shangHuRebate intValue];
    
    /*
     * 用户注册返点
     */
    int userRegisterRebate = [WFModel.userRegisterRebate intValue];
    
    /*
     * 具体彩种返点
     */
    int czRebate = [WFModel.czRebate intValue];
    
    
    /*
     * 彩种投注返点
     */
    int czTZRebate = [WFModel.czTZRebate intValue];
    
    
    /*
     * 商户最小值
     */
    int shangHuMinRebate = [WFModel.shangHuMinRebate intValue];
    
    /*
     * 列表间隔
     */
    int XRebate = [WFModel.XRebate intValue];
    
#pragma mark-计算
    
    //差值= 商户返点 - 用户注册返点
    int  dValue = shangHuRebate-userRegisterRebate;
    
    /*
     * 用户该彩种返点 = 具体彩种返点 - 差值
     */
    int userCZRebate = czRebate-dValue;
    
    /*
     * 列表中最大的值
     */
    int maxListRebate;
    if (userCZRebate>czTZRebate) {
        maxListRebate=czTZRebate;
    }else{
        maxListRebate=userCZRebate;
    }
    
    
    /*
     * 列表中最小的值
     */
    int minListRebate =shangHuMinRebate;

    
    //maxListRebate  最大
//    if (maxListRebate>=1970) {
//
//        minListRebate = 1970;
//
//    }

    
    int count=(maxListRebate-minListRebate)/XRebate;
    
    
    
    
    
    /*
     补充：
     投注返点选择：
     如果自身返点≥ 1970，可选2挡 自身返点和1970【当前最低是商户最低点】
     如果自身返点＜1970，可选2挡 自身返点和商户最低点
     开户配额
     只显示最高的十个点的配额
     如果自身返点≥ 1970，最小配额1970
     如果自身返点＜1970，无限制。
     */
    
    
    
    
//    int betSelRebate,rebateListMaxValue;
//
//    if (userRegisterRebate >= 1970) {
//
//        betSelRebate = userRegisterRebate;
//        rebateListMaxValue = 1970;
//
//    } else {
//
//        betSelRebate = userRegisterRebate;
//        rebateListMaxValue = shangHuMinRebate;
//
//    }
//
//    if (betSelRebate == rebateListMaxValue) {
//
//        MCShowBetModel * model=[[MCShowBetModel alloc]init];
//        model.BetRebate = [NSString stringWithFormat:@"%d",betSelRebate];
//        model.realRebate = [NSString stringWithFormat:@"%.1f", ((userCZRebate - betSelRebate)/2000.0*100.0)];
//        model.virtualRebate = [NSString stringWithFormat:@"%.1f", ((maxListRebate - betSelRebate)/2000.0*100.0)];
//        model.showRebate = [NSString stringWithFormat:@"%@,%@",model.BetRebate,model.realRebate];
//
//        [marr addObject:model];
//
//    } else {
//
//        MCShowBetModel * model1=[[MCShowBetModel alloc]init];
//        model1.BetRebate = [NSString stringWithFormat:@"%d",betSelRebate];
//
//        model1.realRebate = [NSString stringWithFormat:@"%.1f", ((userCZRebate - betSelRebate)/2000.0*100.0)];
//        model1.virtualRebate = [NSString stringWithFormat:@"%.1f", ((maxListRebate - betSelRebate)/2000.0*100.0)];
//        model1.showRebate = [NSString stringWithFormat:@"%@,%@",model1.BetRebate,model1.realRebate];
//        [marr addObject:model1];
//
//        MCShowBetModel * model2=[[MCShowBetModel alloc]init];
//        model2.BetRebate = [NSString stringWithFormat:@"%d",rebateListMaxValue];
//
//        model2.realRebate = [NSString stringWithFormat:@"%.1f", ((userCZRebate - rebateListMaxValue)/2000.0*100.0)];
//        model2.virtualRebate = [NSString stringWithFormat:@"%.1f", ((maxListRebate - rebateListMaxValue)/2000.0*100.0)];
//        model2.showRebate = [NSString stringWithFormat:@"%@,%@",model2.BetRebate,model2.realRebate];
//
//        [marr addObject:model2];
//
//
//
//    }
    
    
    
    
//        for (int i=0 ; i<=count ; i++  ) {
    for (int i=count ; i >= 0; i--  ) {
        
            MCShowBetModel * model=[[MCShowBetModel alloc]init];
    
            /**列表中选择的返点【左边的数值】*/
            model.BetRebate = [NSString stringWithFormat:@"%d",minListRebate+XRebate*i];
    
            //投注时所选
            float tzSelectedRebate=minListRebate+XRebate*i;
    
            /*
             *      用户该彩种 - 投注时所选
             * 如实=—————————————————————— X 100%
             *             2000
             */
            model.realRebate = [NSString stringWithFormat:@"%.1f", ((userCZRebate - tzSelectedRebate)/2000.0*100.0)];
    
    
            /*
             *      列表中第一个值 - 投注时所选
             * 虚拟=—————————————————————— X 100%
             *             200
             */
            model.virtualRebate = [NSString stringWithFormat:@"%.1f", ((maxListRebate - tzSelectedRebate)/2000.0*100.0)];
    
            model.showRebate = [NSString stringWithFormat:@"%@,%@",model.BetRebate,model.realRebate];
    
            
            
            
            [marr addObject:model];
        }
    
//        NSMutableArray * marry=[[NSMutableArray alloc]init];
//        if (marr.count>1) {
//            [marry addObject:marr[marr.count-1]];
//        }
//
//        [marry addObject:marr[0]];
    return marr;
}

+(BOOL)isCanTouZhu{
    
    
    //    MCMineInfoModel * mineInfoModel = [MCMineInfoModel sharedMCMineInfoModel];
    //    NSString *sUserLevel =[NSString stringWithFormat:@"%@",mineInfoModel.UserLevel];
    //    if (sUserLevel.length>0) {
    //        if (([sUserLevel intValue]>0)&&([sUserLevel intValue]<5)) {
    //            return NO;
    //        }
    //    }
    return YES;
}
@end






/*
 
 A  ->  (get_lottery_list)获取商户所有彩种列表
 
 ①MaxRebate    Int    当前彩种可投注的最大返点
 ②BetRebate    Int    商户的投注返点
 
 B  ->  (get_user_detail)获取商户所有彩种列表
 
 ①MyRebate    Int    我（当前登录用户）的返点
 
 
 C  ->  (get_merchant_info)获取商户信息
 
 ① MaxRebate    Int    商户允许投注的最大返点
 ② MinRebate    Int    商户允许投注的最小返点
 ③ XRebate    Int    相邻返点差值
 
 计算：
 
 商户返点率：C①     用户注册返点：B①
 差值Sub= C① - B①
 
 具体彩种返点：A①
 
 彩种投注返点：A②     用户该彩种返点：A①-Sub
 
 
 选项表中的最小值：C②
 选项表中的最大值：将彩种投注返点（A② ）  和 用户该彩种返点（A①-Sub） 进行比较  取最小值
 
 
 用户该彩种（A①-Sub） - 投注时所选
 如实 = ———————————————————————————————————————— X 100%
 2000
 
 
 
 列表中第一个值（选项表中的最大值） - 投注时所选
 虚拟 = ———————————————————————————————————————— X 100%
 2000
 
 
 */
















